package com.ysg.widget.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemChildLongClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.listener.OnItemLongClickListener;
import com.ysg.R;
import com.ysg.base.BaseDialog;
import com.ysg.http.data.entity.expression.ExpressionEntity;
import com.ysg.utils.YCollectionUtil;
import com.ysg.utils.YDialogUtil;
import com.ysg.utils.YRecyclerViewUtil;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;
import com.ysg.widget.dialog.adapter.DialogSheetAdapter;
import com.ysg.widget.dialog.adapter.ExpressionAdapter;
import com.ysg.widget.dialog.entity.DialogEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * 底部 聊天常用语
 */
public class ExpressionDialog extends BaseDialog {
    public interface OnClickListener{
        void onSendExpression(String text);
        void onJumpExpression();
        void onExpressionDelete(String id);
    }
    private OnClickListener listener;
    public void setOnClickListener(OnClickListener listener){
        this.listener = listener;
    }

    private TextView btnCustom;
    private RecyclerView recyclerView;
    private ExpressionAdapter adapter;
    private List<ExpressionEntity> list;

    public ExpressionDialog(Context context, List<ExpressionEntity> list) {
        super(context);
        this.list = list;
    }

    @Override
    public int getLayoutId() {
        return R.layout.dialog_expression;
    }

    public void initView() {
        setWidthScale(1f);
        Window window = getWindow();
        window.setGravity(Gravity.BOTTOM);

        recyclerView = findViewById(R.id.recyclerView);
        adapter = new ExpressionAdapter();
        YRecyclerViewUtil.init(mContext, recyclerView, adapter);

        btnCustom = findViewById(R.id.btnCustom);
    }

    @Override
    public void initData() {
        adapter.setList(list);
    }

    @Override
    public void initListener() {
        btnCustom.setOnClickListener((v -> {
            if (listener != null){
                dismiss();
                listener.onJumpExpression();
            }
        }));
        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                if (listener != null){
                    dismiss();
                    listener.onSendExpression(list.get(position).getContent());
                }
            }
        });
        adapter.setOnItemLongClickListener(new OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                ExpressionEntity entity = list.get(position);
                if (YStringUtil.eq("1", entity.getIsCommon())){
                    YToastUtil.showShort(R.string.tip_expression_common);
                    return true;
                }
                onDelete(entity.getId());
                return true;
            }
        });
    }

    private void onDelete(String id){
        YDialogUtil.getInstance().showConfirm(getContext(), getContext().getResources().getString(R.string.tip_dialog_delete))
                .setOnConfirmListener(()->{
                    if (listener != null){
                        dismiss();
                        listener.onExpressionDelete(id);
                    }
                });
    }



}
